package rpg.writer.app;

import java.awt.BorderLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ScriptJPanel extends JPanel implements ActionListener {
	/**
     * 
     */
	private static final long serialVersionUID = 4763559039046997522L;

	private static void debug(final Object o) {
		System.out.println("ScriptJPanel: " + o);
	}

	private final WriterJFrame writerApp;

	private final TextArea textArea = new TextArea();
	private final JScrollPane scroller = new JScrollPane(textArea);

	private final JButton btnPlayer = new JButton("Play");
	private final JComboBox cbScripts = new JComboBox(new String[] {
			"scripts/tutorial.txt", "scripts/sto.txt" });

	public ScriptJPanel(final WriterJFrame jframe) {
		super(new BorderLayout());
		writerApp = jframe;

		add(cbScripts, BorderLayout.NORTH);
		add(scroller, BorderLayout.CENTER);
		add(btnPlayer, BorderLayout.SOUTH);

		// Set Listeners
		btnPlayer.addActionListener(this);
		cbScripts.addActionListener(this);
	}

	@Override
	public void actionPerformed(final ActionEvent e) {
		debug(e);
		if (e.getSource() == btnPlayer) {
			writerApp.play(textArea.getText());
		} else if (e.getSource() == cbScripts) {
			try {
				setScript(writerApp.loadInternalScript(cbScripts
						.getSelectedItem().toString()));
			} catch (final IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	public void setScript(final String newText) {
		textArea.setText(newText);
	}
}
